package game.zqll;

import com.badlogic.gdx.Files;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.GdxRuntimeException;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class PluginFileHandle extends FileHandle {

    public PluginFileHandle(String fileName) {
        super(fileName, Files.FileType.Classpath);
    }

    @Override
    public InputStream read() {
        if (this.type != Files.FileType.Classpath && this.type != Files.FileType.Internal || this.file().exists() && this.type != Files.FileType.Local || this.file().exists()) {
            try {
                return new FileInputStream(this.file());
            } catch (Exception var2) {
                if (this.file().isDirectory()) {
                    throw new GdxRuntimeException("Cannot open a stream to a directory: " + this.file + " (" + this.type + ")", var2);
                } else {
                    throw new GdxRuntimeException("Error reading file: " + this.file + " (" + this.type + ")", var2);
                }
            }
        } else {
            InputStream input = this.getClass().getResourceAsStream("/" + this.file.getPath().replace('\\', '/'));
            if (input == null) {
                throw new GdxRuntimeException("File not found: " + this.file + " (" + this.type + ")");
            } else {
                return input;
            }
        }
    }

    @Override
    public FileHandle parent() {
        File parent = this.file.getParentFile();
        return new PluginFileHandle(parent.getPath());
    }
}
